<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>表单</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body class="body">


<blockquote class="layui-elem-quote layui-text">
    <%=ret.name%> --> <%=ret.key%>
</blockquote>

<form class="layui-form layui-form-pane" >
    <input type="hidden" name="id" value="<%=ret._id%>" />
    <%
    if(ret.fields.length){
        ret.fields.forEach(function(f){
    %>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 220px"><%=f.name%></label>
        <div class="layui-input-block" style="margin-left: 220px;">
            <%
            let output = [];
            if(f.option) {
                output.push(`<select name="${f.key}">`);
                for (let k in f.option) {
                    let select = k == f.val ? 'selected' : '';
                    let option = `<option value="${k}" ${select}>${f.option[k]}</option>`;
                    output.push(option);
                }
                output.push('</select>');
            }
            else{
                output.push(`<input type="text" name="${f.key}" value="${f.val}" autocomplete="off" placeholder="${f.val}" class="layui-input">`);
            }
            %>
            <%-output.join("\n")%>
        </div>
    </div>
    <%
        });
    }
    %>

    <div class="layui-form-item" style="margin-left: 220px;">
        <button class="layui-btn" lay-submit="" lay-filter="submit">提交</button>
    </div>
</form>

<script src="/layui/layui.js" charset="utf-8"></script>
<%- include('../bpath') %>
<script>
    layui.extend({
            "cosjs": '{/}/layui/?name=cosjs'
    })
    layui.use(['form','cosjs'], function(){
        var form = layui.form,
            cosjs = layui.cosjs;
        var handle = ['branch','opensdk'].join('/');
        var fields_length = <%=ret.fields.length%>;
        form.on('submit(submit)', function(data){
            if(!fields_length){
                return cosjs.layer.close();
            }
            cosjs.post(GURL(handle,'configureSave'), data.field).then(ret=>{
                cosjs.layer.close();
            }).catch(err=>{
                cosjs.alert(err)
            })
            return false;
        });
    });
</script>
</body>
</html>